package com.demo.webapp.kcfinder.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.demo.webapp.kcfinder.result.Result;

public class PackageConfig {

	private String name;
	private String namespace;
	
	protected List<ActionConfig> actionConfigList;
	protected Map<String, InterceptorConfig> interceptorMap;
	protected Map<String, ResultTypeConfig> resultTypeMap;
	protected ClassLoader classLoader;

	private List<InterceptorConfig> interceptorConfigList;
	private List<ResultTypeConfig> resultTypeConfigList;
	
	public PackageConfig() {
		//init context
		actionConfigList = new ArrayList<ActionConfig>();
		
		interceptorMap = new LinkedHashMap<String, InterceptorConfig>();
		resultTypeMap = new HashMap<String, ResultTypeConfig>();
		interceptorConfigList = new ArrayList<InterceptorConfig>();
		resultTypeConfigList = new ArrayList<ResultTypeConfig>();
		classLoader = Thread.currentThread().getContextClassLoader();
	}
	
	public boolean support(String namespace) {
		
		return ("".equals(namespace) || this.namespace.equals(namespace));
	}
	
	public ResultTypeConfig getResultType(String type) {
		return this.resultTypeMap.get(type);
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNamespace() {
		return namespace;
	}

	public void setNamespace(String namespace) {
		this.namespace = namespace;
	}

	public ActionConfig getConfig(String name, String method) {
		//判断是否使用通配符,是则进入模糊查找
		for (ActionConfig config : this.actionConfigList) {
			if (config.support(name, method)) {
				return config;
			}
		}
		throw new ConfigurationException("not found configuration, the name" + name + " and method is " + method );
	}

	public void addConfig(ActionConfig config) {
		actionConfigList.add(config);
	}
	
	protected void putResult(String type, ResultTypeConfig result) {
		resultTypeMap.put(type, result);
	}
	
	public void putInterceptorConfig(String name, InterceptorConfig config) 
			throws InstantiationException, IllegalAccessException, ClassNotFoundException {
//		Interceptor interceptor = null;
//		ClassLoader localeLoader = classLoader;
//		interceptor = (Interceptor) Class.forName(clazz, true, localeLoader != null ? localeLoader : this.getClass().getClassLoader()).newInstance();
//		putInterceptor(name, interceptor);
		//String name = "123";
		interceptorMap.put(config.getName(), config);
	}

	public void setClassLoader(ClassLoader classLoader) {
		this.classLoader = classLoader;
	}

	public Map<String, InterceptorConfig> getInterceptorConfigMap() {
		return interceptorMap;
	}

	public Map<String, ResultTypeConfig> getResultTypeMap() {
		return resultTypeMap;
	}
	
	public void addInterceptorConfig(InterceptorConfig config) {
		interceptorConfigList.add(config);
	}
	
	public void addResultTypeConfig(ResultTypeConfig config) {
		this.resultTypeConfigList.add(config);
	}
	
	public void after() {
		if (interceptorConfigList.size() > 0) {
			for(InterceptorConfig config : interceptorConfigList) {
				interceptorMap.put(config.getName(), config);
			}
		}
		if (resultTypeConfigList.size() > 0) {
			for(ResultTypeConfig config : resultTypeConfigList) {
				resultTypeMap.put(config.getName(), config);
			}
		}
	}
}
